package com.yunao.commons.serachcore.core.bean;

import com.yunao.service.report.bean.compare.ComparatorByKey;
import com.yunao.service.report.bean.compare.ComparatorByValue;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.util.stream.Collectors.joining;

public class Cube implements Serializable {

	private static final long serialVersionUID = 1L;

	private String key;
	private long value;
	private List<Group> subs;

	public Cube() {

	}

	public Cube(String key, long value) {
		this.key = key;
		this.value = value;
	}

	public String getKey() {
		return key;
	}

	public long getValue() {
		return value;
	}

	public List<Group> getSubs() {
		if (subs == null) {
			return Collections.emptyList();
		}
		return subs;
	}

	public void addSub(Group e) {
		if (subs == null) {
			subs = new ArrayList<>();
		}
		subs.add(e);
	}

	public void sortByKey() {
		Collections.sort(subs,new ComparatorByKey<Group>());
	}

	public void sortByValue() {
		Collections.sort(subs, new ComparatorByValue<Group>());
	}

	@Override
	public String toString() {
		StringBuilder res = new StringBuilder(key);
		res.append(" : ").append(value);
		if (subs != null && !subs.isEmpty()) {
			res.append("\t[");
			String substr = subs.stream()
					.map(Group::toString)
					.collect(joining(", "));
			res.append(substr).append("]");
		}
		return res.toString();
	}

}
